import { reactive, ref } from 'vue'
import type { CleanupStaff, CleanupAssignment, CleanupAssignmentForm, CleanupAssignmentSearchForm } from '@/types/cleanup'
import type { TreeSelectOption } from 'element-plus'

// 清运分配页面逻辑
export function useCleanupAssignment() {
  // 搜索表单
  const searchForm = reactive<CleanupAssignmentSearchForm>({
    staffName: '',
    deviceCode: '',
    area: '',
    status: '',
    page: 1,
    size: 10
  })

  // 清运员列表
  const staffList = ref<CleanupStaff[]>([
    {
      id: 1,
      name: '张三',
      phone: '13800138001',
      area: '朝阳区',
      assignedCount: 5,
      status: 'active',
      createdAt: '2024-01-01 08:00:00'
    },
    {
      id: 2,
      name: '李四',
      phone: '13800138002',
      area: '海淀区',
      assignedCount: 3,
      status: 'active',
      createdAt: '2024-01-02 09:00:00'
    },
    {
      id: 3,
      name: '王五',
      phone: '13800138003',
      area: '丰台区',
      assignedCount: 0,
      status: 'inactive',
      createdAt: '2024-01-03 10:00:00'
    },
    {
      id: 4,
      name: '赵六',
      phone: '13800138004',
      area: '西城区',
      assignedCount: 7,
      status: 'active',
      createdAt: '2024-01-04 11:00:00'
    },
    {
      id: 5,
      name: '钱七',
      phone: '13800138005',
      area: '东城区',
      assignedCount: 2,
      status: 'active',
      createdAt: '2024-01-05 12:00:00'
    }
  ])

  // 设备树形数据
  const deviceTreeData = ref<TreeSelectOption[]>([
    {
      value: 'beijing',
      label: '北京市',
      children: [
        {
          value: 'chaoyang',
          label: '朝阳区',
          children: [
            { value: '1001', label: 'DLF-001 (CBD大厦)' },
            { value: '1002', label: 'DLF-002 (国贸中心)' },
            { value: '1003', label: 'DLF-003 (三里屯)' }
          ]
        },
        {
          value: 'haidian',
          label: '海淀区',
          children: [
            { value: '1004', label: 'DLF-004 (中关村)' },
            { value: '1005', label: 'DLF-005 (五道口)' }
          ]
        },
        {
          value: 'xicheng',
          label: '西城区',
          children: [
            { value: '1006', label: 'DLF-006 (金融街)' }
          ]
        }
      ]
    },
    {
      value: 'shanghai',
      label: '上海市',
      children: [
        {
          value: 'pudong',
          label: '浦东新区',
          children: [
            { value: '1007', label: 'DLF-007 (陆家嘴)' }
          ]
        }
      ]
    }
  ])

  // 分配记录列表
  const assignmentList = ref<CleanupAssignment[]>([
    {
      id: 1,
      staffId: 1,
      staffName: '张三',
      deviceId: 1001,
      deviceCode: 'DLF-001',
      deviceLocation: '朝阳区CBD大厦',
      assignedAt: '2024-01-28 09:00:00',
      status: 'assigned'
    },
    {
      id: 2,
      staffId: 1,
      staffName: '张三',
      deviceId: 1002,
      deviceCode: 'DLF-002',
      deviceLocation: '朝阳区国贸中心',
      assignedAt: '2024-01-28 10:00:00',
      status: 'assigned'
    },
    {
      id: 3,
      staffId: 2,
      staffName: '李四',
      deviceId: 1003,
      deviceCode: 'DLF-003',
      deviceLocation: '海淀区中关村',
      assignedAt: '2024-01-28 11:00:00',
      status: 'completed'
    },
    {
      id: 4,
      staffId: 4,
      staffName: '赵六',
      deviceId: 1004,
      deviceCode: 'DLF-004',
      deviceLocation: '西城区金融街',
      assignedAt: '2024-01-28 12:00:00',
      status: 'assigned'
    }
  ])

  // 分配表单
  const assignmentForm = reactive<CleanupAssignmentForm>({
    staffId: undefined,
    deviceIds: [],
    remark: ''
  })

  // 分配对话框显示状态
  const assignmentDialogVisible = ref(false)
  const reassignmentDialogVisible = ref(false)
  const deleteDialogVisible = ref(false)

  // 当前操作的行数据
  const currentRow = ref<CleanupAssignment | null>(null)

  // 搜索处理
  const handleSearch = () => {
    searchForm.page = 1
    loadAssignmentList()
  }

  // 重置搜索
  const handleReset = () => {
    Object.assign(searchForm, {
      staffName: '',
      deviceCode: '',
      area: '',
      status: '',
      page: 1,
      size: 10
    })
    loadAssignmentList()
  }

  // 加载分配记录列表
  const loadAssignmentList = () => {
    // 模拟API调用
    console.log('加载分配记录列表:', searchForm)
  }

  // 分配操作
  const handleAssign = (row: CleanupStaff) => {
    assignmentForm.staffId = row.id
    assignmentDialogVisible.value = true
  }

  // 换人操作
  const handleReassign = (row: CleanupAssignment) => {
    currentRow.value = row
    reassignmentDialogVisible.value = true
  }

  // 删除操作
  const handleDelete = (row: CleanupAssignment) => {
    currentRow.value = row
    deleteDialogVisible.value = true
  }

  // 确认分配
  const confirmAssign = () => {
    console.log('确认分配:', assignmentForm)
    assignmentDialogVisible.value = false
    // 重置表单
    Object.assign(assignmentForm, {
      staffId: undefined,
      deviceIds: [],
      remark: ''
    })
  }

  // 确认换人
  const confirmReassign = () => {
    console.log('确认换人:', currentRow.value)
    reassignmentDialogVisible.value = false
    currentRow.value = null
  }

  // 确认删除
  const confirmDelete = () => {
    console.log('确认删除:', currentRow.value)
    deleteDialogVisible.value = false
    currentRow.value = null
  }

  // 分页处理
  const handlePageChange = (page: number) => {
    searchForm.page = page
    loadAssignmentList()
  }

  // 页面大小改变
  const handleSizeChange = (size: number) => {
    searchForm.size = size
    searchForm.page = 1
    loadAssignmentList()
  }

  return {
    searchForm,
    staffList,
    assignmentList,
    deviceTreeData,
    assignmentForm,
    assignmentDialogVisible,
    reassignmentDialogVisible,
    deleteDialogVisible,
    currentRow,
    handleSearch,
    handleReset,
    handleAssign,
    handleReassign,
    handleDelete,
    confirmAssign,
    confirmReassign,
    confirmDelete,
    handlePageChange,
    handleSizeChange
  }
}
